﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RubberStopperDoubleKo : BaseObj
{
    public GameObject m_Target;
    public Transform m_FatherTarget;
    public Transform m_BigFather;

    public GameObject m_RubberDropper;//

    public GameObject m_Flask;

    public static RubberStopperDoubleKo Instance;

    private void Awake()
    {
        Instance = this;
    }

    public bool OnFlask;
    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        if (OnFlask && RoundBottpmFlask.Instance.OnPlatfrom && !RoundBottpmFlask.HaveStopper)
        {
            OnRoundBottpmFlask();
        }
        else
        {
            base.OnReleseTrigger(handleType);
            transform.SetParent(m_BigFather);
            if (!RoundBottpmFlask.Instance.OnPlatfrom)
            {
                StepsManager.Instance.StepFourNum[1] = true;
                StepsManager.Instance.SetStepFour(1);
            }
            if (StepsManager.Instance.StepFourNum[0])
            {
                if ((transform.position - positon).magnitude < 0.5f)
                {
                    StepsManager.Instance.StepFiveNum[2] = true;
                    StepsManager.Instance.SetStepFive(2);
                }
            }
            m_RubberDropper.transform.localScale = new Vector3(1f, 1f, 1f);
        }
    }

    public void OnRoundBottpmFlask()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
        transform.position = m_Target.transform.position;
        transform.SetParent(m_FatherTarget);
        StepsManager.Instance.StepTwoNum[1] = true;
        StepsManager.Instance.SetStepTwo(1);
    }


    bool FlaskIsBroken = false;
    private void LateUpdate()
    {
        if (Vector3.Distance(m_Target.transform.position, transform.position) < 0.1f)
        {
            OnFlask = true;
        }
        else
        {
            OnFlask = false;
        }

        if (m_Flask.activeInHierarchy && FlaskIsBroken)
        {
            FlaskIsBroken = false;
        }
        else if (!m_Flask.activeInHierarchy && !FlaskIsBroken)
        {
            FlaskIsBroken = true;
            SetOnGround();
        }

        if (RoundBottpmFlask.Instance.m_HaveReadWater && !RoundBottpmFlask.Instance.m_IsOnGround)
            canCatsh = false;
        else
            canCatsh = true;
    }

    public void SetStartMove(bool needRound = false)
    {
        if (needRound)
            transform.rotation = Quaternion.Euler(0, 0, 0);
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
    }

    public void ResetParent()
    {
        transform.SetParent(m_BigFather);
    }

    public void SetStopMove()
    {
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        OnRoundBottpmFlask();
    }

    public void SetOnGround()
    {
        OnFlask = false;
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
        transform.rotation = Quaternion.Euler(90, 0, 0);
        ResetParent();
    }

    //记录每个大步骤的初始状态
    #region
    public void SetBigStepTwo()
    {
        ResetPosition();
        transform.rotation = Quaternion.Euler(90, 0, 0);
        transform.SetParent(m_BigFather);
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
        m_RubberDropper.transform.localScale = new Vector3(1f, 1f, 1f);
    }

    public void SetBigStepThree()
    {
        m_RubberDropper.transform.localScale = new Vector3(1f, 1f, 1f);
        transform.rotation = Quaternion.Euler(0, 0, 0);
        OnRoundBottpmFlask();
    }

    public void SetBigStepFour()
    {
        transform.rotation = Quaternion.Euler(0, 0, 0);
        OnRoundBottpmFlask();
    }

    public void SetBigStepFive()
    {

    }
    #endregion

    [HideInInspector]
    public bool m_InWater;
    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "BeakerCollider")
        {
            m_InWater = true;
            if (!WaterStopClip.OpenJiaZi)
            {
                StepsManager.Instance.StepTwoNum[2] = true;
                StepsManager.Instance.SetStepTwo(2);
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        Debug.LogWarning(other.name);
        if (other.name == "BeakerCollider")
        {
            m_InWater = false;
        }
    }

    [HideInInspector]
    public bool m_OnRay;
    public override void OnRayEnter()
    {
        base.OnRayEnter();
        m_OnRay = true;
    }

    public override void OnRayExit()
    {
        base.OnRayExit();
        m_OnRay = false;
    }
}
